import InteractionUtil from '../../shell/ui/interaction';
import SessionUserUtil from '../../data/session/sessionUser'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    address: '',
    latitude: '',
    longitude: '',
    point: '',
    mobile:'',
    real_name:'',
    shop_name:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const {
      type
    } = options
    if (type && type * 1 === 3) {
      this.queryData()
    } else {
      SessionUserUtil.getInstance().then(sessionUser => {
        const {_data} = sessionUser
        const {realName,phoneNumber} = _data
        this.setData({
          real_name:realName,
          mobile: phoneNumber
        })
      })
    }
  },
  queryData() {
    wx.$http().request({
      url: 'AppMaster/SellingServer/getWholesaleLicenseStatus',
      success: res => {
        const {
          data,
          result
        } = res.data
        if (result === 0) {
          const {latitude, longitude, mobile, real_name, shop_name,shop_address} = data
          this.setData({
            mobile,
            latitude,
            longitude,
            real_name,
            shop_address,
            shop_name,
            address: shop_address,
            point: `${longitude},${latitude}`
          })
        }
      }
    })
  },
  submitForm(e) {
    const {
      value
    } = e.detail
    const {
      shop_name,
      real_name,
      mobile,
      shop_address
    } = value
    const {
      latitude,
      longitude
    } = this.data
    if (shop_name.trim() === '') {
      InteractionUtil.showNotice('请输入店铺名称')
      return
    }
    if (real_name.trim() === '') {
      InteractionUtil.showNotice('请输入姓名')
      return
    }
    if (mobile.trim() !== '' && !(/^1[3456789]\d{9}$/.test(mobile))) {
      InteractionUtil.showNotice('手机号码格式输入不正确')
      return
    }
    const params = {
      shop_name,
      real_name,
      mobile,
      shop_address,
      latitude,
      longitude
    }
    wx.$http().request({
      url: 'AppMaster/SellingServer/wholesaleLicense',
      data: params,
      method:'POST',
      params,
      success: res => {
        const {
          data
        } = res
        const {
          message,
          result
        } = data
        if (result !== 0) {
          InteractionUtil.showNotice(message)
        } else {
          InteractionUtil.showSuccess('提交成功!').then(() => {
            wx.navigateBack()
          })
        }
      }
    })
  },
  openLocation() {
    wx.chooseLocation({
      success: (res) => {
        console.log(res)
        const {
          latitude,
          longitude,
          address
        } = res
        this.setData({
          latitude,
          longitude,
          address,
          point: `${longitude},${latitude}`
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})